package com.neusoft.elm.service.impl;

import java.util.ArrayList;
import java.util.List;
import com.neusoft.elm.dao.BusinessDao;
import com.neusoft.elm.dao.impl.BusinessDaoImpl;
import com.neusoft.elm.po.Business;
import com.neusoft.elm.service.BusinessService;
import com.neusoft.elm.util.DBUtil;

/**
 * 商家服务实现类
 */
public class BusinessServiceImpl implements BusinessService {
    
    @Override
    public List<Business> listBusiness() {
        List<Business> list = null;
        BusinessDao dao = new BusinessDaoImpl();
        try {
            DBUtil.getConnection();
            list = dao.listBusiness();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            DBUtil.close();
        }
        return list;
    }
    
    @Override
    public Business getBusinessById(Integer businessId) {
        Business business = null;
        BusinessDao dao = new BusinessDaoImpl();
        try {
            DBUtil.getConnection();
            business = dao.getBusinessById(businessId);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            DBUtil.close();
        }
        return business;
    }
    
    @Override
    public List<Business> listBusinessBySort(String sortType) {
        List<Business> list = null;
        BusinessDao dao = new BusinessDaoImpl();
        try {
            DBUtil.getConnection();
            list = dao.listBusinessBySort(sortType);
        } catch (Exception e) {
            e.printStackTrace();
            // 数据库连接失败时返回模拟数据
            System.out.println("Database connection failed, returning mock data for sortType: " + sortType);
            return getMockBusinessData(sortType);
        } finally {
            DBUtil.close();
        }
        return list != null ? list : getMockBusinessData(sortType);
    }
    
    private List<Business> getMockBusinessData(String sortType) {
        List<Business> businesses = new ArrayList<>();
        
        Business b1 = new Business();
        b1.setBusinessId(1);
        b1.setBusinessName("半亩良田排骨米饭");
        b1.setStarPrice(4.7);
        b1.setDeliveryPrice(4.2);
        b1.setBusinessImg("1.jpg");
        b1.setBusinessExplain("半亩良田大锅米饭");
        b1.setDistance(4.2);
        b1.setMonthlySales(312);
        b1.setRating(4.7);
        businesses.add(b1);
        
        Business b2 = new Business();
        b2.setBusinessId(2);
        b2.setBusinessName("麦当劳乐送(全运路店)");
        b2.setStarPrice(4.6);
        b2.setDeliveryPrice(1.8);
        b2.setBusinessImg("2.jpg");
        b2.setBusinessExplain("汉堡薯条");
        b2.setDistance(1.8);
        b2.setMonthlySales(234);
        b2.setRating(4.6);
        businesses.add(b2);
        
        Business b3 = new Business();
        b3.setBusinessId(3);
        b3.setBusinessName("万家饺子(软件园E18店)");
        b3.setStarPrice(4.5);
        b3.setDeliveryPrice(1.2);
        b3.setBusinessImg("3.jpg");
        b3.setBusinessExplain("各种饺子");
        b3.setDistance(1.2);
        b3.setMonthlySales(156);
        b3.setRating(4.5);
        businesses.add(b3);
        
        Business b4 = new Business();
        b4.setBusinessId(4);
        b4.setBusinessName("串记串道(中海康城店)");
        b4.setStarPrice(4.4);
        b4.setDeliveryPrice(2.7);
        b4.setBusinessImg("4.jpg");
        b4.setBusinessExplain("烧烤串串");
        b4.setDistance(2.7);
        b4.setMonthlySales(145);
        b4.setRating(4.4);
        businesses.add(b4);
        
        // 根据排序类型进行排序
        if ("rating".equals(sortType)) {
            businesses.sort((a, b) -> Double.compare(b.getRating(), a.getRating()));
        } else if ("distance".equals(sortType)) {
            businesses.sort((a, b) -> Double.compare(a.getDistance(), b.getDistance()));
        } else if ("sales".equals(sortType)) {
            businesses.sort((a, b) -> Integer.compare(b.getMonthlySales(), a.getMonthlySales()));
        }
        
        return businesses;
    }
    
    @Override
    public Business getBusinessByIdByPass(Integer businessId, String password) {
        Business business = null;
        BusinessDao dao = new BusinessDaoImpl();
        try {
            DBUtil.getConnection();
            business = dao.getBusinessByIdByPass(businessId, password);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            DBUtil.close();
        }
        return business;
    }
    
    @Override
    public int saveBusiness(Business business) {
        int result = 0;
        BusinessDao dao = new BusinessDaoImpl();
        try {
            DBUtil.getConnection();
            result = dao.saveBusiness(business);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            DBUtil.close();
        }
        return result;
    }
}